package scau.mis4.chanyulin.view;

import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import scau.mis4.chanyulin.dao.OtherDao;
import scau.mis4.chanyulin.service.DataExportService;
import scau.mis4.chanyulin.service.OutdoorTempService;
import scau.mis4.chanyulin.tools.PeriodTask;
import scau.mis4.chanyulin.tools.utils.UiUtils;
import java.io.File;

/**
 * Created by cyl on 2018/1/3.
 * 主界面
 */
public class MainStage extends Stage {

    public MainStage() {

        // 图像显示仪
        SensorDisplay sensorDisplay = new SensorDisplay();
        sensorDisplay.setLayoutX(5.0);
        sensorDisplay.setLayoutY(5.0);

        // 传感器温度
        Text senTempLabel = new Text("传感器温度");
        Text senTempVal = new Text();
        senTempVal.textProperty().bind(Global.senTempProp);
        senTempVal.setStyle("-fx-font-size: 34px");
        VBox senTempTxtBox = new VBox(senTempLabel, senTempVal);
        senTempTxtBox.setPadding(new Insets(15, 0, 0, 5));
        Global.senTempImgBox.setPadding(new Insets(0, 5, 0, 10));

        HBox hBox1 = new HBox(Global.senTempImgBox, senTempTxtBox);
        hBox1.setPrefSize(150.0, 78.0);
        hBox1.setLayoutX(305.0);
        hBox1.setLayoutY(5.0);

        // 室外温度
        Text outTempLabel = new Text("室外温度[" + Global.city + "]");
        Text outTempVal = new Text();
        outTempVal.textProperty().bind(Global.outTempProp);
        outTempVal.setStyle("-fx-font-size: 20px");
        VBox outTempTxtBox = new VBox(outTempLabel, outTempVal);
        outTempTxtBox.setPadding(new Insets(15, 0, 0, 5));
        Global.outTempImgBox.setPadding(new Insets(0, 5, 0, 10));

        HBox hBox2 = new HBox(Global.outTempImgBox, outTempTxtBox);
        hBox2.setPrefSize(150.0, 78.0);
        hBox2.setLayoutX(305.0);
        hBox2.setLayoutY(100.0);

        // 数据库
        Text dbLabel = new Text("数据库使用");
        Text dbVal = new Text();
        dbVal.textProperty().bind(Global.dbProp);
        dbVal.setStyle("-fx-font-size: 20px");
        VBox dbTxtBox = new VBox(dbLabel, dbVal);
        dbTxtBox.setPadding(new Insets(15, 0, 0, 5));
        Global.dbImgBox.setPadding(new Insets(0, 5, 0, 10));

        HBox hBox3 = new HBox(Global.dbImgBox, dbTxtBox);
        hBox3.setPrefSize(150.0, 78.0);
        hBox3.setLayoutX(305.0);
        hBox3.setLayoutY(195.0);

        // 设置和导出按钮
        HBox hBox4 = new HBox(Global.EXP_DARKGREY, Global.SETTINGS_DARKGREY);
        hBox4.setPrefSize(150.0, 25);
        hBox4.setLayoutX(320.0);
        hBox4.setLayoutY(280.0);
        hBox4.setAlignment(Pos.CENTER_RIGHT);
        hBox4.setSpacing(10.0);
        Global.EXP_DARKGREY.setCursor(Cursor.HAND);
        Global.EXP_GREY.setCursor(Cursor.HAND);
        Global.EXP_DARKGREY.setOnMouseEntered(e -> {hBox4.getChildren().set(0, Global.EXP_GREY);});
        Global.EXP_GREY.setOnMouseExited(e -> {hBox4.getChildren().set(0, Global.EXP_DARKGREY);});
        // 数据导出Excel
        Global.EXP_GREY.setOnMouseClicked(e -> {
            File target = Global.DATA_EXPORT_DIR_CHOOSER.showDialog(null);
            if (target != null && target.getPath() != null && !"".equals(target.getPath())) {
                boolean result = DataExportService.exportAllDataToExcel(target.getPath());
                UiUtils.AlertByBool(result, "数据导出Excel成功", "数据导出Excel失败");
            }
        });
        Global.SETTINGS_DARKGREY.setCursor(Cursor.HAND);
        Global.SETTINGS_GREY.setCursor(Cursor.HAND);
        Global.SETTINGS_DARKGREY.setOnMouseEntered(e -> {hBox4.getChildren().set(1, Global.SETTINGS_GREY);});
        Global.SETTINGS_GREY.setOnMouseExited(e -> {hBox4.getChildren().set(1, Global.SETTINGS_DARKGREY);});
        // 设置
        //TODO
        Global.SETTINGS_GREY.setOnMouseClicked(e -> {
            new AlertStage("Ooops~程序员偷懒了，这个功能好像还没实现...").show();
        });

        // 主界面
        Pane mainPane = new Pane(sensorDisplay, hBox1, hBox2, hBox3, hBox4);
        mainPane.setPrefSize(470.0, 300.0);
        Scene scene = new Scene(mainPane);
        this.setScene(scene);
        this.setTitle("温度传感器显示仪");
        this.setResizable(false);

        // 开启图像输出
        sensorDisplay.start();

        /** 定时任务 */
        // 数据库
        PeriodTask.createAndSchedule(() -> {
            Platform.runLater(() -> {
                if (Global.DB_CONN_CHECKER.isNetworkAvailable()) {
                    Double dataSize = OtherDao.getDataSize();
                    if (dataSize == null) {
                        Global.dbImgBox.getChildren().set(0, Global.DB_GREY);
                        dbVal.setStyle("-fx-font-size: 12px");
                        Global.dbProp.setValue("数据获取失败");
                    } else {
                        Global.dbImgBox.getChildren().set(0, Global.DB_GREEN);
                        dbVal.setStyle("-fx-font-size: 20px");
                        Global.dbProp.setValue(dataSize + "MB");
                        System.out.println("[数据库使用大小] " + dataSize + "MB");
                    }
                } else {
                    Global.dbImgBox.getChildren().set(0, Global.DB_GREY);
                    dbVal.setStyle("-fx-font-size: 12px");
                    Global.dbProp.setValue("数据获取失败");
                }
            });
        }, 10.0);

        // 室外温度
        PeriodTask.createAndSchedule(() -> {
            Platform.runLater(() -> {
                if (Global.OUT_TEMP_CONN_CHECKER.isNetworkAvailable()) {
                    Integer outTemp = OutdoorTempService.getOutTemp(Global.city);
                    if (outTemp == null) {
                        Global.outTempImgBox.getChildren().set(0, Global.OUT_GREY);
                        outTempVal.setStyle("-fx-font-size: 12px");
                        Global.outTempProp.setValue("数据获取失败");
                    } else {
                        Global.outTempImgBox.getChildren().set(0, Global.OUT_GREEN);
                        outTempVal.setStyle("-fx-font-size: 20px");
                        Global.outTempProp.setValue(outTemp + "℃");
                        System.out.println("[实时室外温度] " + outTemp + "℃");
                    }
                } else {
                    Global.outTempImgBox.getChildren().set(0, Global.OUT_GREY);
                    outTempVal.setStyle("-fx-font-size: 12px");
                    Global.outTempProp.setValue("数据获取失败");
                }
            });
        }, 10.0);

    }

}
